#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Author: 深圳星河软通科技有限公司 A.Star
# @contact: astar@snowland.ltd
# @site: www.astar.ltd
# @file: cmp .py
# @time: 2020/11/15 18:10
# @Software: PyCharm

from easydict import EasyDict
import gmssl
from gmssl import (
    sm2, sm3, sm4
)
import pysmx
import time
import unittest
from astartool.random import random_string


class TestSM3(unittest.TestCase):
    def setUp(self):
        self.pysmx_sm3 = pysmx.SM3
        self.gmssl_sm3 = sm3

    def test_hash_100(self):
        ss = random_string(100)
        s = ss.encode()
        pysmx_start_time = time.process_time()
        pysmx_hash = self.pysmx_sm3.hexdigest(s)
        pysmx_end_time = time.process_time()
        gmssl_start_time = time.process_time()
        gmssl_ss = [ord(e) for e in ss]
        gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
        gmssl_end_time = time.process_time()
        print("pysmx:", pysmx_hash)
        print("gmssl:", gmssl_hash)
        self.assertEquals(pysmx_hash, gmssl_hash)
        print('100bytes:')
        print("pysmx_time:", pysmx_end_time - pysmx_start_time)
        print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    def test_signverify_1000(self):
        ss = random_string(1000)
        s = ss.encode()
        pysmx_start_time = time.process_time()
        pysmx_hash = self.pysmx_sm3.hexdigest(s)
        pysmx_end_time = time.process_time()
        gmssl_start_time = time.process_time()
        gmssl_ss = [ord(e) for e in ss]
        gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
        gmssl_end_time = time.process_time()
        print("pysmx:", pysmx_hash)
        print("gmssl:", gmssl_hash)
        self.assertEquals(pysmx_hash, gmssl_hash)
        print('1000bytes:')
        print("pysmx_time:", pysmx_end_time - pysmx_start_time)
        print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    def test_signverify_10000(self):
        ss = random_string(10000)
        s = ss.encode()
        pysmx_start_time = time.process_time()
        pysmx_hash = self.pysmx_sm3.hexdigest(s)
        pysmx_end_time = time.process_time()
        gmssl_start_time = time.process_time()
        gmssl_ss = [ord(e) for e in ss]
        gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
        gmssl_end_time = time.process_time()
        print("pysmx:", pysmx_hash)
        print("gmssl:", gmssl_hash)
        self.assertEquals(pysmx_hash, gmssl_hash)
        print('10000bytes:')
        print("pysmx_time:", pysmx_end_time - pysmx_start_time)
        print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    def test_signverify_100000(self):
        ss = random_string(100000)
        s = ss.encode()
        pysmx_start_time = time.process_time()
        pysmx_hash = self.pysmx_sm3.hexdigest(s)
        pysmx_end_time = time.process_time()
        gmssl_start_time = time.process_time()
        gmssl_ss = [ord(e) for e in ss]
        gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
        gmssl_end_time = time.process_time()
        print("pysmx:", pysmx_hash)
        print("gmssl:", gmssl_hash)
        self.assertEquals(pysmx_hash, gmssl_hash)
        print('100bytes:')
        print("pysmx_time:", pysmx_end_time - pysmx_start_time)
        print("gmssl_time:", gmssl_end_time - gmssl_start_time)


class TestSM4(unittest.TestCase):
    pass

if __name__ == '__main__':
    # 100
    self = EasyDict()
    self.pysmx_sm3 = pysmx.SM3
    self.gmssl_sm3 = sm3
    ss = random_string(100)
    s = ss.encode()
    pysmx_start_time = time.process_time()
    pysmx_hash = self.pysmx_sm3.hexdigest(s)
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_ss = [ord(e) for e in ss]
    gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
    gmssl_end_time = time.process_time()
    print("pysmx:", pysmx_hash)
    print("gmssl:", gmssl_hash)
    assert (pysmx_hash == gmssl_hash)
    print('100bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)

    # 1000
    ss = random_string(1000)
    s = ss.encode()
    pysmx_start_time = time.process_time()
    pysmx_hash = self.pysmx_sm3.hexdigest(s)
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_ss = [ord(e) for e in ss]
    gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
    gmssl_end_time = time.process_time()
    print("pysmx:", pysmx_hash)
    print("gmssl:", gmssl_hash)
    assert (pysmx_hash == gmssl_hash)
    print('1000bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)

# 10000
    ss = random_string(10000)
    s = ss.encode()
    pysmx_start_time = time.process_time()
    pysmx_hash = self.pysmx_sm3.hexdigest(s)
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_ss = [ord(e) for e in ss]
    gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
    gmssl_end_time = time.process_time()
    print("pysmx:", pysmx_hash)
    print("gmssl:", gmssl_hash)
    assert (pysmx_hash == gmssl_hash)
    print('10000bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)

# 100000
    ss = random_string(100000)
    s = ss.encode()
    pysmx_start_time = time.process_time()
    pysmx_hash = self.pysmx_sm3.hexdigest(s)
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_ss = [ord(e) for e in ss]
    gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
    gmssl_end_time = time.process_time()
    print("pysmx:", pysmx_hash)
    print("gmssl:", gmssl_hash)
    assert (pysmx_hash == gmssl_hash)
    print('100000bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)

# 1000000
    ss = random_string(1000000)
    s = ss.encode()
    pysmx_start_time = time.process_time()
    sm3 = self.pysmx_sm3.SM3(s)
    pysmx_hash = sm3.hexdigest()
    pysmx_end_time = time.process_time()
    gmssl_start_time = time.process_time()
    gmssl_ss = [ord(e) for e in ss]
    gmssl_hash = self.gmssl_sm3.sm3_hash(gmssl_ss)
    gmssl_end_time = time.process_time()
    print("pysmx:", pysmx_hash)
    print("gmssl:", gmssl_hash)
    assert (pysmx_hash == gmssl_hash)
    print('1000000bytes:')
    print("pysmx_time:", pysmx_end_time - pysmx_start_time)
    print("gmssl_time:", gmssl_end_time - gmssl_start_time)